package com.by.controller;

import org.springframework.core.SpringVersion;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LittleDesire
 * @description
 * @date 2022-03-25 周五 9:45
 * @since 17
 */
@RestController @RequestMapping("api") @CrossOrigin
public class ApiController {

    @GetMapping("a1")
    public String a1() {
        return String.format("hello 中俄文 %s", SpringVersion.getVersion());
    }

    @GetMapping("a2")
    public void a2(PrintWriter out) {
        out.println("zhonwgen中文");
    }


    // 文.件[/WEB-INF/templates/api/a3.jsp] 未找到
    // 用 ModelAndView model 可以在用@RestController 依然让该请求去寻找视图（需要配视图解析器）
    @GetMapping("a3")
    public ModelAndView a3(ModelAndView mv) {
        mv.setViewName("a3");
        mv.addObject("msg", "成功");
        return mv;
    }


    @GetMapping("a4")
    public Map<String, Object> a4(Integer id , String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        return map;
    }

    @PostMapping("a5")
    public Map<String, Object> a5(Integer id , String name, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        System.out.println(req.getMethod());
        return map;
    }

    @DeleteMapping("a6")
    public Map<String, Object> a6(@RequestBody Map<String, Object> map, HttpServletRequest req) {
        System.out.println(req.getMethod());
        System.out.println(map);
        return map;
    }


    @PostMapping("a7")
    public Map<String, Object> a7(String name, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        System.out.println(req.getMethod());
        System.out.println(map);
        return map;
    }


    @PostMapping({"a8","a8/{i}.html"})
    public Map<String, Object> a8(
            @RequestParam(name = "nn",required = false,defaultValue = "李四") String name,
            @PathVariable(name = "i",required = false) Integer id,
            HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("id", id);
        System.out.println(req.getMethod());
        System.out.println(map);
        return map;
    }






    }
